﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
namespace He.DAL
{
	/// <summary>
	/// 数据访问类:attr_field
	/// </summary>
	public partial class attr_field
	{
		public attr_field()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQLite.GetMaxID("id", "he_attr_field"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from he_attr_field");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(He.Model.attr_field model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into he_attr_field(");
			strSql.Append("channel_id,title,name,control_type,data_type,editor_type,default_value,tip_msg,sort_id,is_required,is_lock,is_sys)");
			strSql.Append(" values (");
			strSql.Append("@channel_id,@title,@name,@control_type,@data_type,@editor_type,@default_value,@tip_msg,@sort_id,@is_required,@is_lock,@is_sys)");
			strSql.Append(";select LAST_INSERT_ROWID()");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@channel_id", DbType.Int32,8),
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@name", DbType.String),
					new SQLiteParameter("@control_type", DbType.String),
					new SQLiteParameter("@data_type", DbType.String),
					new SQLiteParameter("@editor_type", DbType.Int32,8),
					new SQLiteParameter("@default_value", DbType.String),
					new SQLiteParameter("@tip_msg", DbType.String),
					new SQLiteParameter("@sort_id", DbType.Int32,8),
					new SQLiteParameter("@is_required", DbType.Int32,8),
					new SQLiteParameter("@is_lock", DbType.Int32,8),
					new SQLiteParameter("@is_sys", DbType.Int32,8)};
			parameters[0].Value = model.channel_id;
			parameters[1].Value = model.title;
			parameters[2].Value = model.name;
			parameters[3].Value = model.control_type;
			parameters[4].Value = model.data_type;
			parameters[5].Value = model.editor_type;
			parameters[6].Value = model.default_value;
			parameters[7].Value = model.tip_msg;
			parameters[8].Value = model.sort_id;
			parameters[9].Value = model.is_required;
			parameters[10].Value = model.is_lock;
			parameters[11].Value = model.is_sys;

			object obj = DbHelperSQLite.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(He.Model.attr_field model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update he_attr_field set ");
			strSql.Append("channel_id=@channel_id,");
			strSql.Append("title=@title,");
			strSql.Append("name=@name,");
			strSql.Append("control_type=@control_type,");
			strSql.Append("data_type=@data_type,");
			strSql.Append("editor_type=@editor_type,");
			strSql.Append("default_value=@default_value,");
			strSql.Append("tip_msg=@tip_msg,");
			strSql.Append("sort_id=@sort_id,");
			strSql.Append("is_required=@is_required,");
			strSql.Append("is_lock=@is_lock,");
			strSql.Append("is_sys=@is_sys");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@channel_id", DbType.Int32,8),
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@name", DbType.String),
					new SQLiteParameter("@control_type", DbType.String),
					new SQLiteParameter("@data_type", DbType.String),
					new SQLiteParameter("@editor_type", DbType.Int32,8),
					new SQLiteParameter("@default_value", DbType.String),
					new SQLiteParameter("@tip_msg", DbType.String),
					new SQLiteParameter("@sort_id", DbType.Int32,8),
					new SQLiteParameter("@is_required", DbType.Int32,8),
					new SQLiteParameter("@is_lock", DbType.Int32,8),
					new SQLiteParameter("@is_sys", DbType.Int32,8),
					new SQLiteParameter("@id", DbType.Int32,8)};
			parameters[0].Value = model.channel_id;
			parameters[1].Value = model.title;
			parameters[2].Value = model.name;
			parameters[3].Value = model.control_type;
			parameters[4].Value = model.data_type;
			parameters[5].Value = model.editor_type;
			parameters[6].Value = model.default_value;
			parameters[7].Value = model.tip_msg;
			parameters[8].Value = model.sort_id;
			parameters[9].Value = model.is_required;
			parameters[10].Value = model.is_lock;
			parameters[11].Value = model.is_sys;
			parameters[12].Value = model.id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_attr_field ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_attr_field ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public He.Model.attr_field GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,channel_id,title,name,control_type,data_type,editor_type,default_value,tip_msg,sort_id,is_required,is_lock,is_sys from he_attr_field ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			He.Model.attr_field model=new He.Model.attr_field();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["channel_id"]!=null && ds.Tables[0].Rows[0]["channel_id"].ToString()!="")
				{
					model.channel_id=int.Parse(ds.Tables[0].Rows[0]["channel_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["title"]!=null && ds.Tables[0].Rows[0]["title"].ToString()!="")
				{
					model.title=ds.Tables[0].Rows[0]["title"].ToString();
				}
				if(ds.Tables[0].Rows[0]["name"]!=null && ds.Tables[0].Rows[0]["name"].ToString()!="")
				{
					model.name=ds.Tables[0].Rows[0]["name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["control_type"]!=null && ds.Tables[0].Rows[0]["control_type"].ToString()!="")
				{
					model.control_type=ds.Tables[0].Rows[0]["control_type"].ToString();
				}
				if(ds.Tables[0].Rows[0]["data_type"]!=null && ds.Tables[0].Rows[0]["data_type"].ToString()!="")
				{
					model.data_type=ds.Tables[0].Rows[0]["data_type"].ToString();
				}
				if(ds.Tables[0].Rows[0]["editor_type"]!=null && ds.Tables[0].Rows[0]["editor_type"].ToString()!="")
				{
					model.editor_type=int.Parse(ds.Tables[0].Rows[0]["editor_type"].ToString());
				}
				if(ds.Tables[0].Rows[0]["default_value"]!=null && ds.Tables[0].Rows[0]["default_value"].ToString()!="")
				{
					model.default_value=ds.Tables[0].Rows[0]["default_value"].ToString();
				}
				if(ds.Tables[0].Rows[0]["tip_msg"]!=null && ds.Tables[0].Rows[0]["tip_msg"].ToString()!="")
				{
					model.tip_msg=ds.Tables[0].Rows[0]["tip_msg"].ToString();
				}
				if(ds.Tables[0].Rows[0]["sort_id"]!=null && ds.Tables[0].Rows[0]["sort_id"].ToString()!="")
				{
					model.sort_id=int.Parse(ds.Tables[0].Rows[0]["sort_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["is_required"]!=null && ds.Tables[0].Rows[0]["is_required"].ToString()!="")
				{
					model.is_required=int.Parse(ds.Tables[0].Rows[0]["is_required"].ToString());
				}
				if(ds.Tables[0].Rows[0]["is_lock"]!=null && ds.Tables[0].Rows[0]["is_lock"].ToString()!="")
				{
					model.is_lock=int.Parse(ds.Tables[0].Rows[0]["is_lock"].ToString());
				}
				if(ds.Tables[0].Rows[0]["is_sys"]!=null && ds.Tables[0].Rows[0]["is_sys"].ToString()!="")
				{
					model.is_sys=int.Parse(ds.Tables[0].Rows[0]["is_sys"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,channel_id,title,name,control_type,data_type,editor_type,default_value,tip_msg,sort_id,is_required,is_lock,is_sys ");
			strSql.Append(" FROM he_attr_field ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM he_attr_field ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from he_attr_field T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "he_attr_field";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

